﻿using DomainService.IService.Sys;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model.BusinessModel;
using Model.BusinessModel.InPut;
using Model.BusinessModel.MatchFormula;
using Model.BusinessModel.OutPut.sys;
using Model.DomainModel.Sys;
using Utility;

namespace OverallAuth_WebApi.Controllers.Sys
{
    /// <summary>
    /// SysDataRule 数据规则
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "SysDataRule")]
    public class SysDataRuleController : BaseController
    {
        #region 构造实列化

        /// <summary>
        /// 数据规则服务
        /// </summary>
        public readonly ISysDataRuleService _sysDataRuleService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sysDataRuleService"></param>
        public SysDataRuleController(ISysDataRuleService sysDataRuleService)
        {
            _sysDataRuleService = sysDataRuleService;
        }

        #endregion

        #region 接口

        /// <summary>
        /// 新增规则数据
        /// </summary>
        /// <param name="sysDataRule">规则数据</param>
        [HttpPost]
        public ReceiveStatus Insert(SysDataRuleInput sysDataRule)
        {
            sysDataRule.CreateTime = DateTime.Now;
            sysDataRule.CreateUser = GetLoginUserMsg().UserId.ToString();
            return _sysDataRuleService.Insert(sysDataRule, GetLoginUserMsg());
        }

        /// <summary>
        /// 修改规则数据
        /// </summary>
        /// <param name="sysDataRule">规则数据</param>
        [HttpPost]
        public ReceiveStatus Update(SysDataRuleInput sysDataRule)
        {
            sysDataRule.CreateTime = DateTime.Now;
            sysDataRule.CreateUser = GetLoginUserMsg().UserId.ToString();
            return _sysDataRuleService.Update(sysDataRule, GetLoginUserMsg());
        }

        /// <summary>
        /// 设置规则是否启用
        /// 同一个菜单、同一个规则类型，只能启用一个规则
        /// </summary>
        /// <param name="ruleId">规则主键</param>
        /// <param name="menuId">菜单id</param>
        /// <param name="isOpen">是否打开</param>
        /// <param name="rowAuthType">行权限类型</param>
        [HttpGet]
        public ReceiveStatus SetRuleIsOpenById(string? ruleId, string? menuId, bool isOpen, int rowAuthType)
        {
            return _sysDataRuleService.SetRuleIsOpenById(ruleId, menuId, isOpen, rowAuthType);
        }

        /// <summary>
        /// 获取菜单数据规则数据
        /// </summary>
        /// <param name="menuId">菜单ID</param>
        /// <param name="ruleType">规则类型</param>
        /// <returns>返回规则数据</returns>
        [HttpGet]
        public ReceiveStatus<SysDataRuleOutPut> GetMenuDataRuleList(string menuId, int ruleType)
        {
            return _sysDataRuleService.GetMenuDataRuleList(menuId, ruleType);
        }

        /// <summary>
        ///  获取数据规则
        /// </summary>
        /// <param name="pageResultModel">分页模型</param>
        /// <returns>返回规则数据</returns>
        [HttpPost]
        public ReceiveStatus<SysDataRuleOutPut> GetDataRuleList(PageResultModel pageResultModel)
        {
            ReceiveStatus<SysDataRuleOutPut> receiveStatus = new();
            var result = _sysDataRuleService.GetDataRuleList(pageResultModel);
            receiveStatus.data = result.data;
            receiveStatus.total = result.total;
            return receiveStatus;
        }

        /// <summary>
        /// 获取当前规则使用按钮
        /// </summary>
        /// <param name="ruleId"></param>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<string> GetUseDataRuleButton(string ruleId)
        {
            return _sysDataRuleService.GetUseDataRuleButton(ruleId);
        }

        /// <summary>
        /// 获取数据权限配置
        /// </summary>
        /// <param name="menuId">菜单id</param>
        /// <param name="configType">配置类型</param>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<MatchFormulaDataSourceOutPut> GetDataRuleConfig(string menuId, int configType)
        {
            return _sysDataRuleService.GetDataRuleConfig(menuId, configType);
        }
        #endregion
    }
}
